﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework0304
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Init();
        }

        public void Init()
        {
            //初始化界面设置
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            //设置文本框不能由用户修改
            this.SetNumReadOnly(true);
        }

        private void SetNumReadOnly(bool isReadOnly)
        {
            this.tb_1.ReadOnly = isReadOnly;
            this.tb_2.ReadOnly = isReadOnly;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tb_1.Text = null; //初始化输入框
            tb_2.Text = null;
            double calcAdd = 0;
            double[] calcArea = new double[] {0};
             for (int i = 0; i < 10; i++)
              {
                 Random rd_0 = new Random();
                 int test = rd_0.Next(1, 10);
                 if (test % 2 == 0)
                  {
                      Shape s = new Rectangle();
                      s.vendor = "矩形";
                      double result = s.Area();
                      calcArea = calcArea.Concat(new double[] { result }).ToArray();
                      tb_1.AppendText("矩形，面积为：" + result + "\r\n");
                  }
                 else
                  {
                      Shape s = new Circle();
                      s.vendor = "圆";
                      double result = s.Area();
                      calcArea = calcArea.Concat(new double[] { result }).ToArray();
                      tb_1.AppendText("圆，面积为：" + result + "\r\n");
                    }
                System.Threading.Thread.Sleep(200);
              }
             for(int j = 0; j < calcArea.Length; j++)
            {
                calcAdd = calcAdd + calcArea[j];
            }
            tb_2.Text = calcAdd.ToString();
        }
    }

    public abstract class Shape
    {
        public string vendor { get; set; }
        public abstract int sideCount{ get; } //边长数量
        public abstract double Area(); //求面积方法

    }
    public class Rectangle : Shape
    {
        public override int sideCount
        {
            get
            {
                if (vendor == "矩形")
                    return 2;
                else
                    return 0;
            }
        }
        public override double Area()
        {
            Random rd1 = new Random();
            int a = rd1.Next(1,100);
            Random rd2 = new Random();
            int b = rd2.Next(1,100);
            double area = a * b;
            return area;
        }
    }

    public class Circle : Shape
    {
        public override int sideCount
        {
            get
            {
                if (vendor == "圆")
                    return 1;
                else
                    return 0;
            }
        }
        public override double Area()
        {
            Random rd = new Random();
            int radius = rd.Next(1,100);
            double c = 3.14;
            double area = radius * 3.14;
            return area;
        }
    }
}
